﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HKEA.CMS.Core.IDAL.App;
using HKEA.CMS.Core.Models;
using HKEA.CMS.Core.DALFactory;
namespace HKEA.CMS.Core.DAL.App
{
    public class app_mtblBrandDAL:Iapp_mtblBrand
    {
        HKEA_CMSEntities model;
        app_mtblBrand brand;
        private app_mtblBrandDAL()
        { 
            model = DataAccess.GetModelInstance();
        }

        public static app_mtblBrandDAL GetInstance()
        {
            return new app_mtblBrandDAL();
        }

        public int InsertENT(app_mtblBrand ent)
        {
            ent.CreationDate = DateTime.Now;
            ent.LastModifyDate = DateTime.Now;
            model.app_mtblBrand.AddObject(ent);
            if (model.SaveChanges() > 0)
                return model.app_mtblBrand.Max(a => a.AppBrandUID);
            return 0;
        }

        public app_mtblBrand InsertENTRetENT(app_mtblBrand ent)
        {
            ent.CreationDate = DateTime.Now;
            ent.LastModifyDate = DateTime.Now;
            model.app_mtblBrand.AddObject(ent);
            if (model.SaveChanges() > 0)
                return ent = model.app_mtblBrand.OrderByDescending(a => a.AppBrandUID).FirstOrDefault();
            return null;
        }

        public List<app_mtblBrand> GetListByENT(app_mtblBrand ent, bool IsAddDeleteForSelect)
        {
            var list = model.app_mtblBrand.Where(a => a.AppBrandUID > 0);
            if (ent.CustomerUID > 0)
                list = list.Where(a => a.CustomerUID == ent.CustomerUID);
            if (ent.BrandUID > 0)
                list = list.Where(a => a.BrandUID == ent.BrandUID);
            if (ent.AppBrandUID > 0)
                list = list.Where(a => a.AppBrandUID == ent.AppBrandUID);
            if (IsAddDeleteForSelect)
                list = list.Where(a => a.Deleted == ent.Deleted);
            return list.ToList();
        }

        public List<M_BrandListDetail> GetListByENT(int CustomerUID)
        {
            var ret = from app_brand in model.app_mtblBrand
                      where app_brand.CustomerUID==CustomerUID && app_brand.Deleted==0
                      select new M_BrandListDetail
                      {
                          AppBrandLogo = app_brand.AppMediaUIDBrandLogo ?? 0,
                          AppBrandUID = app_brand.AppBrandUID,
                          BrandLogo = app_brand.BrandLogo ?? 0,
                          BrandName_Chi = app_brand.BrandName_Chi,
                          BrandName_Eng = app_brand.BrandName_Eng,
                          BrandUID = app_brand.BrandUID ?? 0,
                          Deleted = app_brand.Deleted,
                          IsFromApp = true
                      };
            return ret.ToList();
        }

        /// <summary>
        /// 此方法暂不使用
        /// </summary>
        /// <param name="ent"></param>
        /// <returns></returns>
        public List<BrandListDetail> GetListDetailByENT(app_mtblBrand ent)
        {
            var Result = new List<BrandListDetail>();
            var appBrand = from tbappBrand in model.app_mtblBrand
                           where tbappBrand.CustomerUID == ent.CustomerUID
                           select new BrandListDetail
                           {
                               SourceType = "app",
                               Deleted = tbappBrand.Deleted,
                               BrandLogo = tbappBrand.BrandLogo,
                               CustomerUID = tbappBrand.CustomerUID,
                               BrandUID = tbappBrand.BrandUID ?? 0,
                               BrandName_Chi = tbappBrand.BrandName_Chi,
                               BrandName_Eng = tbappBrand.BrandName_Eng
                           };
            var AddOrUpdateList = new List<BrandListDetail>(); //新增的实体
            var UpdateUIDList = new List<int>(); //被修改的实体，在结果集中mtblBrand表将排除
            var DeleteUIDList = new List<int>(); //删除的实体
            foreach (var tempENT in appBrand)
            {
                if (tempENT.Deleted == 0)
                {
                    if (tempENT.BrandUID == 0)
                        Result.Add(tempENT);
                    else
                        UpdateUIDList.Add(tempENT.BrandUID);
                }
                else
                    DeleteUIDList.Add(tempENT.BrandUID);
            }
            DeleteUIDList.AddRange(UpdateUIDList);
            var removeListUID = DeleteUIDList.Distinct().ToList();

            var brand = from tbbrand in model.mtblBrands
                        where !removeListUID.Contains(tbbrand.BrandUID)
                         select new BrandListDetail
                         {
                             BrandLogo = tbbrand.BrandLogo,
                             CustomerUID = tbbrand.CustomerUID,
                             BrandUID = tbbrand.BrandUID,
                             BrandName_Chi = tbbrand.BrandName_Chi,
                             BrandName_Eng = tbbrand.BrandName_Eng
                         };
            if (ent.CustomerUID > 0)
                brand = brand.Where(a => a.CustomerUID == ent.CustomerUID);
            if (ent.BrandUID > 0)
                brand = brand.Where(a => a.BrandUID == ent.BrandUID);
            return brand.ToList();
        }

        public List<app_mtblBrand> GetListByENT(app_mtblBrand ent)
        {
            var result = model.app_mtblBrand.Where(a => a.AppBrandUID > 0).Where(a => a.Deleted == ent.Deleted);
            if (ent.CustomerUID > 0)
                result = result.Where(a => a.CustomerUID == ent.CustomerUID);
            if (ent.BrandUID > 0)
                result = result.Where(a => a.BrandUID == ent.BrandUID);
            if (ent.AppBrandUID > 0)
                result = result.Where(a => a.AppBrandUID == ent.AppBrandUID);
            return result.ToList();
        }

        public int GetCountByENT(app_mtblBrand ent)
        {
            throw new NotImplementedException();
        }
         
        public int UpdateENT(app_mtblBrand ent)
        {
            var temp = model.app_mtblBrand.Where(a => a.AppBrandUID == ent.AppBrandUID).FirstOrDefault();
            if (temp != null)
            {
                ent.AppMediaUIDBrandLogo = ent.AppMediaUIDBrandLogo;
                ent.BrandLogo = ent.BrandLogo;
                ent.BrandName_Chi = ent.BrandName_Chi;
                ent.BrandName_Eng = ent.BrandName_Eng;
                ent.BrandUID = ent.BrandUID;
                ent.Deleted = ent.Deleted;
                ent.Intro_Chi = ent.Intro_Chi;
                ent.Intro_Eng = ent.Intro_Eng;
                ent.LastModifyDate = DateTime.Now;
            }
            return model.SaveChanges();
        }

        public int DeleteENT(app_mtblBrand ent)
        {
             var temp = model.app_mtblBrand.Where(a => a.AppBrandUID == ent.AppBrandUID).FirstOrDefault();
             if (temp != null)
             {
                 model.app_mtblBrand.DeleteObject(temp);
             }
             return model.SaveChanges();
        }

        public List<int> GetCustomerUIDList()
        {
            return model.app_mtblBrand.Where(a=>a.CustomerUID > 0).Select(a=>a.CustomerUID).ToList();
        }

        public int DeleteByKeyList(List<int> list)
        {
            throw new NotImplementedException();
        }
    }
}
